
//
// 121226 - AH - Created.
//

package org.japril.swing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Frame;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;

/**
 * <p>
 *   {@link JFrame} for quick testing of widgets.
 * </p> 
 * @author Arne Halvorsen (AH) 
 */
@SuppressWarnings("serial")
public final class TestFrame extends JFrame 
{
  /**
   * <p>
   *   Creates and shows frame.
   * </p>
   * <p>
   *   Width and height of frame will be default.
   * </p>
   * <p>
   *   Clients uses
   *   {@link #show(Component)} to show frame in thread safe way.
   * </p>
   * @param c {@link Component} to show.
   * @param w Width of frame, if {@code -1} both width and height is default.
   * @param h Height of frame, if {@code -1} both width and height is default.
   */
  public TestFrame(Component c){ this(c, -1, -1); }
  
  /**
   * <p>
   *   Creates and shows frame.
   * </p>
   * <p>
   *   Clients uses
   *   {@link #show(Component, int, int)} to show frame in thread safe way.
   * </p>
   * @param c {@link Component} to show.
   * @param w Width of frame, if {@code -1} both width and height is default.
   * @param h Height of frame, if {@code -1} both width and height is default.
   */
  private TestFrame(Component c, int w, int h)
  {
    if (c==null)
    {
      throw new NullPointerException("c");
    }
    
    Container cp=getContentPane();
    cp.setLayout(new BorderLayout());
    
    add(c, BorderLayout.CENTER);
    
    pack();
    if (w>0 && h>0) setSize(w, h);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  
  /**
   * <p>
   *   Creates and shows frame.
   * </p>
   * <p>
   *   Clients uses
   *   {@link #show(JMenu)} to show frame in thread safe way.
   * </p> 
   * @param menu Menu to show.
   */
  private TestFrame(JMenu menu)
  {
    if (menu==null)
    {
      throw new NullPointerException("menu");
    }
    
    JMenuBar mb=new JMenuBar();
    mb.add(menu);
    setJMenuBar(mb);
    
    pack();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  
  /**
   * <p>
   *   Creates and shows frame.
   * </p>
   * <p>
   *   Width and height of frame will be default.
   * </p>
   * @param c {@link Component} to show.
   * @param w Width of frame, if {@code -1} both width and height is default.
   * @param h Height of frame, if {@code -1} both width and height is default.
   */
  public static void show(Component c){ show(c, -1, -1); }
  
  /**
   * <p>
   *   Creates and shows frame.
   * </p>
   * @param c {@link Component} to show.
   * @param w Width of frame, if {@code -1} both width and height is default.
   * @param h Height of frame, if {@code -1} both width and height is default.
   */
  public static void show(Component c, int w, int h)
  { 
    show(new TestFrame(c, w, h));
  }
  
  /**
   * <p>
   *   Creates and shows frame.
   * </p>
   * @param menu {@link JMenu} to show.
   */
  public static void show(JMenu menu){ show(new TestFrame(menu)); }
  
  /**
   * <p>
   *   Shows dialog.
   * </p>
   * @param dlg Dialog to show.
   */
  public static void show(final Dialog dlg)
  {
    if (dlg==null)
    {
      throw new NullPointerException("dlg");
    }
    
    SwingUtilities.invokeLater
    (
      new Runnable() 
      {
        public void run() 
        {
          dlg.setVisible(true);  
        }
      }
    );
  }
  
  /**
   * <p>
   *   Shows frame.
   * </p>
   * @param f Frame to show.
   */
  public static void show(final Frame f)
  {
    if (f==null)
    {
      throw new NullPointerException("f");
    }
    
    SwingUtilities.invokeLater
    (
      new Runnable() 
      {
        public void run() 
        {
          f.setVisible(true);  
        }
      }
    );
  }
  
}
